$(function () {
	$('#public-head').load('common-head.html?'+ Math.random());
	$('#public-footer').load('common-footer.html?'+ Math.random());
	var Mey = $("#Mey");
	//新加入的商品
	if ($.cookie("laneige")) {
		var jsonObj = eval('('+$.cookie("laneige")+')')
		var goodsImg = jsonObj[0].goodsImg;
		var goodsName = jsonObj[0].goodsName;
		var place = jsonObj[0].place.split("￥")[1];
		var goodsNum = jsonObj[0].goodsNum;
		createTR(goodsImg,goodsName,place,goodsNum);
	}
	//总价钱
	function changeMey() {
		var total = 0;//总金额
		var Count = $(".count-min");
		for(var i = 0; i<Count.length; i++){
			total = Count.eq(i).text().trim();
		}
		Mey.text( total);
	}
	//cookie中的商品添加到页面
	function createTR(goodsImg,goodsName,place,goodsNum) {
		var money = Number(place)*Number(goodsNum);
		var TR = '';
		TR += '<td><div><input type="checkbox" name="" checked="checked"></div><div class="goodsImg"><img src="'+goodsImg+'"></div>';
		TR += '<div><p class="goodsName">'+goodsName+'</p><p>规格：瓶</p></div></td>';
		TR += '<td class="unit-price">'+place+'</td><td class="goods-Number"><a class="sub-num" href="javascript:;">-</a><input type="text" name="" value="'+goodsNum+'"><a class="add-num" href="javascript:;">+</a>';
		TR += '</td><td class="count-min">'+money+'</td><td class="del-goods"><a href="">删除</a></td>'
		$("<tr>").appendTo('tbody');
		$("tr:last").html(TR);
		changeMey();
		_but();
	}
	//商品数量
	//加减
	function _but() {
		var $addnum = $(".add-num");
		var $subnum = $(".sub-num");
		//点击增加
		$($addnum).on("click",function () {
			//商品数量的对象
			var goodsNum = $(this).prev();
			//小计td对象	
			var minCount = $(this).parent(".goods-Number").next(".count-min");
			//单价td对象的值.split("￥")[1]
			var unitPrice =Number( $(this).parents(".goods-Number").prev(".unit-price").text().trim());
			//商品数量增加
			$(goodsNum).val(parseInt(goodsNum.val())+1);
			//小计增加
			$(minCount).text("￥" + unitPrice*goodsNum.val());
			changeMey();
		})
		//点击减少
		$($subnum).on("click",function () {
			//商品数量
			var goodsNum = $(this).next();
			//小计
			var minCount = $(this).parent(".goods-Number").next(".count-min");
			//单价.split("￥")[1]
			var unitPrice =Number( $(this).parents(".goods-Number").prev(".unit-price").text().trim());
			if (goodsNum.val() == 0) {
				return false;
			}else{
				$(goodsNum).val(parseInt(goodsNum.val())-1);
				$(minCount).text("￥" + unitPrice*goodsNum.val());
				changeMey();
			}
		})
	
	//点击删除
	var $delgoods = $(".del-goods a");
	$delgoods.on("click",function(){
		$(this).parents("tr").html("");
		changeMey();
		//应该删除对应的cookie
	})
	}
	
	
})
				